function Console() {
}

Console.Type = {
  LOG: "log",
  DEBUG: "debug",
  INFO: "info",
  WARN: "warn",
  ERROR: "error",
  GROUP: "group",
  GROUP_COLLAPSED: "groupCollapsed",
  GROUP_END: "groupEnd"
};

Console.addMessage = function(type, format, args) {
  chrome.extension.sendRequest({
      command: "sendToConsole",
      tabId: chrome.devtools.tabId,
      args: escape(JSON.stringify(Array.prototype.slice.call(arguments, 0)))
  });
};

(function() {
  var console_types = Object.getOwnPropertyNames(Console.Type);
  for (var type = 0; type < console_types.length; ++type) {
    var method_name = Console.Type[console_types[type]];
    Console[method_name] = Console.addMessage.bind(Console, method_name);
  }
})();

Array.prototype.contains = function (obj) {
    var i = this.length;
    while (i--) {
        if (this[i] === obj) {
            return true;
        }
    }
    return false;
}

var ips = new Array();

chrome.devtools.network.onRequestFinished.addListener(
    function(request) {
        if(request.serverIPAddress != null && request.serverIPAddress != "" && request.request.url != null && request.request.url != ""){
            if (!ips.contains(request.serverIPAddress)) {
                ips.push(request.serverIPAddress);
                var regx = /^https?:\/\/([a-z0-9\-\.]+)[\/\?]?/;
                var rs = regx.exec(request.request.url);
                var line = document.createElement("p");
                line.textContent = request.serverIPAddress + " " + rs[1];
                document.getElementById("hosts").appendChild(line);
            }
        }
});